<?php

class AdminPermissionController extends Controller
{
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  
				'actions'=>array('index'),
				'users'=>array('@'),
				'expression'=>'checkAdmin(Yii::app()->user->id)'
			),
			
			array('deny',  // deny$ all users
				'users'=>array('*'),
			),
		);
	}
	public function actionIndex()
	{
		$role = Role::model()->findAll(array('order'=>'role'));
		$controller = AdminController::model()->findAllByAttributes(array('enabled'=>1), array('order'=>'controller'));
		if(Yii::app()->request->isPostRequest) {
			if(isset($_POST['role_per']) && !empty($_POST['role_per'])) {
				AdminPermission::model()->deleteAll();
				foreach ($_POST['role_per'] as $i) {
					$split = explode('_',$i);
					$model=new AdminPermission();
					$model->roleID = $split[0];
					$model->actionID = $split[1];
					$model->controllerID = $split[2];
					$model->save();
				}
				Yii::app()->user->setFlash('success','Cập nhật thành công');
				$this->redirect(Yii::app()->baseUrl.'/admin');
			}
		}
		$this->render('index', array('role'=>$role, 'controller'=>$controller,));
	}

}